package woa.sort.test20241118;

import java.util.Arrays;

/**
 * @author wangpeng
 * @version 1.0
 * @date 2024/11/18 19:55
 */
public class QuickSort {

    public static void main(String[] args) {
        int[] arr = new int[]{4,3,5,6,71};
        int n = arr.length;
        if (n <= 1) {
            return;
        }
        sort(arr, 0, n - 1);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int pi = partition(arr, left, right);
        sort(arr, left, pi - 1);
        sort(arr, pi + 1, right);
    }

    private static int partition(int[] arr, int left, int right) {
        int pivot = arr[right];
        int i = left;
        for (int j = left; j < right; j++) {
            if (arr[j] < pivot) {
                int temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
                i++;
            }
        }
        int temp = arr[i];
        arr[i] = pivot;
        arr[right] = temp;
        return i;
    }
}
